<template>
<transition name="slide">
  <div class="user-center">
    <div class="back" @click="back">
      <i class="icon-back"></i>
    </div>
    <div class="switches-wrapper">
      <switches :switches="switches" @selected="selectSwitch" :currentIndex="currentIndex"></switches>
    </div>
    <div class="play-btn" @click="random">
      <i class="icon-play"></i>
      <span class="text">随机播放全部</span>
    </div>
    <div class="list-wrapper">
      <scroll :data="favoriteList" v-show="currentIndex === 0" class="favoriteList" ref="favoriteList">
          <song-list :songs="favoriteList" @select="selectItem"></song-list>
      </scroll>
      <scroll :data="playHistory" v-show="currentIndex === 1" class="historyList" ref="historyList">
          <song-list :songs="playHistory" @select="selectItem"></song-list>
      </scroll>
    </div>
    <no-result v-show="noResult" class="noResult" :title="noResultDes"></no-result>
  </div>
</transition>
</template>
<script>
import Switches from 'base/switches/switches'
import Scroll from 'base/scroll/scroll'
import SongList from 'base/song-list/song-list'
import {playerMixin, playlistMixin} from 'common/js/mixin'
import Song from 'common/js/songs'
import {mapActions} from 'vuex'
import NoResult from 'base/no-result/no-result'
export default {
  name: 'UserCenter',
  mixins: [playerMixin, playlistMixin],
  data () {
    return {
      switches: [{name: '我喜欢的'}, {name: '最近听的'}],
      currentIndex: 0
    }
  },
  computed: {
    noResult () {
      if (this.currentIndex === 0) {
        return !this.favoriteList.length
      } else {
        return !this.playHistory.length
      }
    },
    noResultDes () {
      if (this.currentIndex === 0) {
        return '暂无搜藏歌曲'
      } else {
        return '您还没有听过歌曲'
      }
    }
  },
  methods: {
    random () {
      let list = this.currentIndex === 0 ? this.favoriteList : this.playHistory
      if (list.length === 0) return
      list = list.map((item) => {
        return new Song(item)
      })
      this.randomPlay({list})
    },
    handlePlaylist (playlist) {
      const bottom = playlist.length > 0 ? '120px' : ''
      this.$refs.favoriteList.$el.style.bottom = bottom
      this.$refs.favoriteList.refresh()
      this.$refs.historyList.$el.style.bottom = bottom
      this.$refs.historyList.refresh()
    },
    selectItem (item, index) {
      this.insertSong(new Song(item))
    },
    selectSwitch (index) {
      this.currentIndex = index
      setTimeout(() => {
        if (this.currentIndex === 0) {
          this.$refs.favoriteList.refresh()
        } else {
          this.$refs.historyList.refresh()
        }
      }, 20)
    },
    back () {
      this.$router.back()
    },
    ...mapActions(['randomPlay'])
  },
  components: {
    Switches,
    Scroll,
    SongList,
    NoResult
  }
}
</script>
<style lang="scss" scoped>
@import 'common/scss/variable.scss';
.user-center{
  position: fixed;
  top: 0;
  bottom: 0;
  width: 100%;
  z-index: 100;
  background: $color-background;
  &.slide-enter-active, &.slide-leave-active{
    transition: all 0.3s;
  }
  &.slide-enter, &.slide-leave-to{
    transform: translate3d(100%, 0, 0)
  }
  .back{
    position: absolute;
    top: 0;
    left: 0.12rem;
    z-index: 50;
    .icon-back{
      display: block;
      padding: 0.2rem;
      font-size: $font-size-large-x;
      color: $color-theme;
    }
  }
  .switches-wrapper{
    margin: 0.2rem 0 0.6rem 0;
  }
  .play-btn{
    box-sizing: border-box;
    width: 2.7rem;
    padding: 0.14rem 0;
    margin:  0 auto;
    text-align: center;
    border: 1px solid $color-text-l;
    color: $color-text-l;
    border-radius: 2rem;
    font-size: 0;
    .icon-play{
      display: inline-block;
      vertical-align: middle;
      margin-right: 0.12rem;
      font-size: $font-size-medium-x;
    }
    .text{
      display: inline-block;
      vertical-align: middle;
      font-size: $font-size-small;
    }
  }
  .list-wrapper{
    .favoriteList, .historyList{
      position: absolute;
      top: 2rem;
      bottom: 0;
      width: 100%;
      overflow: hidden;
    }
  }
  .noResult{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
}
</style>
